package com.spzx.user.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.user.domain.Region;
import com.spzx.user.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.api.domain.UserAddress;
import com.spzx.user.service.IUserAddressService;

/**
 * 用户地址Service业务层处理
 *
 * @author atguigu
 * @date 2024-11-01
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService
{
    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private RegionMapper regionMapper;

    /**
     * 查询用户地址列表
     *
     *
     * @return 用户地址
     */
    @Override
    public List<UserAddress> selectUserAddressList( )
    {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //带条件的查询
        List<UserAddress> userAddressList = baseMapper.selectList(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId));
        return userAddressList;
    }

    @Override
    public List<UserAddress> getUserAddressList(Long userId) {
        return userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId));
    }

    //添加用户地址
    @Override
    public void addUserAddress(UserAddress userAddress) {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //设置用户id
        userAddress.setUserId(userId);
        //获取三级联动省市区的编号
        String provinceCode = userAddress.getProvinceCode();
        String cityCode = userAddress.getCityCode();
        String districtCode = userAddress.getDistrictCode();
        //获取三级联动省市区的名字
        String provinceName = getRegionNameByCode(provinceCode);
        String cityName = getRegionNameByCode(cityCode);
        String districtName = getRegionNameByCode(districtCode);
        //拼接详细地址
        String fulAddress = provinceName + cityName + districtName + userAddress.getAddress();
        //设置详细地址
        userAddress.setFullAddress(fulAddress);
        //判断是否为默认地址,如果为默认地址,把其他地址设置为非默认地址
        if (userAddress.getIsDefault()==1){
            //带条件的修改,
            baseMapper.update(null,new LambdaUpdateWrapper<UserAddress>().eq(UserAddress::getUserId,userId).eq(UserAddress::getIsDefault,1).set(UserAddress::getIsDefault,0));
        }

        //添加默认地址
        baseMapper.insert(userAddress);
    }

    //修改
    @Override
    public void updateUserAddress(UserAddress userAddress) {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //设置用户id
        userAddress.setUserId(userId);
        //获取三级联动省市区的编号
        String provinceCode = userAddress.getProvinceCode();
        String cityCode = userAddress.getCityCode();
        String districtCode = userAddress.getDistrictCode();
        //获取三级联动省市区的名字
        String provinceName = getRegionNameByCode(provinceCode);
        String cityName = getRegionNameByCode(cityCode);
        String districtName = getRegionNameByCode(districtCode);
        //拼接详细地址
        String fulAddress = provinceName + cityName + districtName + userAddress.getAddress();
        //设置详细地址
        userAddress.setFullAddress(fulAddress);
        //判断是否为默认地址,如果为默认地址,把其他地址设置为非默认地址
        if (userAddress.getIsDefault()==1){
            //带条件的修改,
            baseMapper.update(null,new LambdaUpdateWrapper<UserAddress>().eq(UserAddress::getUserId,userId).eq(UserAddress::getIsDefault,1).set(UserAddress::getIsDefault,0));
        }
        //修改地址
        baseMapper.updateById(userAddress);
    }

    @Override
    public void delecteNotDefaultAddressById(Long id) {
        //判断是否为默认地址
        UserAddress userAddress = baseMapper.selectById(id);
        if (userAddress.getIsDefault()==1){
            throw new ServiceException("该地址为默认地址,不能删除");
        }
        //删除
        baseMapper.deleteById(id);
    }

    //根据地区编号获取地区名字的方法
    public String getRegionNameByCode(String code) {
        //调用RegionMapper中带条件查询的方法
        Region region = regionMapper.selectOne(new LambdaQueryWrapper<Region>().eq(Region::getCode, code));
        //获取地区的名字
        return region.getName();
    }
}
